package Blood.SqlParse;

import javafx.scene.control.Tab;
import org.apache.calcite.prepare.Prepare;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class DB {


    public static Connection getConnection() throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.cj.jdbc.Driver");

        return DriverManager.getConnection("jdbc:mysql://192.168.6.131:3306/blood?useSSL=false","root","root");
    }

    public static Statement getStmt(Connection conn) throws SQLException {
        return conn.createStatement();
    }

    public static PreparedStatement getPreparedStmt(String insert, Connection conn ){
        try {
            return conn.prepareStatement(insert);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void insertBatch(PreparedStatement preparedStatement , List<TableNode> nodes){
        try {
            preparedStatement.getConnection().setAutoCommit(false);
            for (int i = 0; i < nodes.size(); i++) {
                preparedStatement.setObject(1,nodes.get(i).getExecute_platform());
                preparedStatement.setObject(2,nodes.get(i).getScript_path());
                preparedStatement.setObject(3,nodes.get(i).getSource_table());
                preparedStatement.setObject(4,nodes.get(i).getTable_name());
                preparedStatement.addBatch();
            }

            preparedStatement.executeBatch();
            preparedStatement.getConnection().commit();

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
    public static ResultSet getResultSet(String sql , Statement stmt) throws SQLException {
        return stmt.executeQuery(sql);
    }
    public static void update(String sql, Statement stmt ){
        try {
            stmt.executeUpdate(sql) ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Connection conn) {
        if(null != conn) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Statement stmt ) {
        if(null != stmt) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(PreparedStatement preparedStatement){
        if(null != preparedStatement){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(ResultSet rs ) {
        if(null != rs) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Connection connection = getConnection();
//        Statement stmt = getStmt(connection);
        PreparedStatement preparedStmt = getPreparedStmt("insert into blood.blood_node(execute_platform,script_path,source_table,table_name) " +
                "values(?,?,?,?)", connection);

//        update("insert into blood.blood_node(pid,execute_platform,script_path,source_table,table_name,updated_at) " +
//                "values(0,'hive','','source_table','table','1234')", stmt);
        List<TableNode> nodes = new ArrayList<TableNode>();
        TableNode tn1 = new TableNode();
        tn1.setSource_table("s1");
        tn1.setTable_name("t1");
        tn1.setScript_path("sdsf");
        tn1.setExecute_platform("hive");
        nodes.add(tn1) ;

        TableNode tn2 = new TableNode();
        tn2.setSource_table("s2");
        tn1.setTable_name("t2");
        tn1.setScript_path("sdsf");
        tn1.setExecute_platform("hive");
        nodes.add(tn1) ;

        insertBatch(preparedStmt,nodes);
        close(connection);
        close(preparedStmt);
    }
}
